<?php
$db = new PDO('mysql:host=127.0.0.1; dbname=db3', 'root', '12qwas');
$ps = $db->query('select * from students',PDO::FETCH_ASSOC); // PDO::FETCH_NUM
table($ps);
function table(PDOStatement $ps) {
    $cc=$ps->columnCount();
    if($cc===0)return;
    echo "<table border='1' cellspacing='0' cellpadding='6'><tr>";
    for ($i = 0; $i < $cc; ++$i) {
        echo '<th>', $ps->getColumnMeta($i)['name'], '</th>';
    }
    echo "</tr>";
    foreach ($ps as $r) {
        echo '<tr>';
        for ($i = 0; $i < $cc; ++$i) {
            echo '<td>', $r[$i]??$r[$ps->getColumnMeta($i)['name']], '</td>';
        }
        echo '</tr>';
    }
    echo "</table>";
}